package com.the8thfold.anton.services

import com.the8thfold.anton.settings.Configuration

class SettingsService {

    def mailService

    static transactional = true

    def updateMailSettings() {
        Configuration config = Configuration.findByName("Mail Server")
        def itemsMap = config.asMap();
        org.codehaus.groovy.grails.commons.ConfigurationHolder.config.grails.mail = new ConfigObject(
                host: itemsMap.host,//"smtp.gmail.com",
                port: itemsMap.port, //465,
                username: itemsMap.userName,
                password: itemsMap.password,
                props: ["mail.smtp.auth": itemsMap.'mail.smtp.auth',//"true",
                        "mail.smtp.socketFactory.port": itemsMap.'mail.smtp.socketFactory.port',//"465",
                        "mail.smtp.socketFactory.class":itemsMap.'mail.smtp.socketFactory.class',//"javax.net.ssl.SSLSocketFactory",
                        "mail.smtp.socketFactory.fallback":itemsMap.'mail.smtp.socketFactory.fallback' ])//"false"])

        mailService.mailMessageBuilderFactory.mailSender.host = itemsMap.host //"smtp.gmail.com"
        mailService.mailMessageBuilderFactory.mailSender.port = Integer.valueOf(itemsMap.port) //465
        mailService.mailMessageBuilderFactory.mailSender.username = itemsMap.userName
        mailService.mailMessageBuilderFactory.mailSender.password = itemsMap.password
        mailService.mailMessageBuilderFactory.mailSender.javaMailProperties.putAll(org.codehaus.groovy.grails.commons.ConfigurationHolder.config.grails.mail.props)
    }
}
